package rz.dmc.sample;

import rz.dmc.R;
import rz.dmc.effect.RZDrawingView;
import rz.dmc.effect.RZDynamicEditText;
import rz.dmc.effect.RZOverlayViewGroup;
import rz.dmc.effect.RZStickerView;
import rz.dmc.util.ColorPickerDialog;
import rz.dmc.util.RZFrame;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements ColorPickerDialog.OnColorChangedListener
{
	private static final int MENU_COLOR = 1;
	private static final int MENU_CLEAR = 2;
	private static final int MENU_ERASE = 3;
	private static final int MENU_STROKE_WIDTH_10 = 4;
	private static final int MENU_STROKE_WIDTH_20 = 5;
	private static final int MENU_ADD_STICKER = 6;
	
	private FrameLayout					viewContainer;
	private LinearLayout.LayoutParams	layoutParams;
	
	private RZDrawingView drawingView;
	private RZOverlayViewGroup overlayView;
	private RZDynamicEditText dynamicEditText;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
//		setContentView(R.layout.activity_main);
		
		drawingView = new RZDrawingView(this, null);
		
		dynamicEditText = new RZDynamicEditText(this, null);
		
		overlayView = new RZOverlayViewGroup(this, null);
		overlayView.addView(dynamicEditText, new RZFrame(100, 100, 300, 100));
		
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.girl);
        RZStickerView stickerView = new RZStickerView(this, bitmap);
		
		
        viewContainer = new FrameLayout(this);
        viewContainer.setBackgroundColor(Color.WHITE);
		
		layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
//		container.addView(drawingView, layoutParams);
		viewContainer.addView(stickerView, layoutParams);
//		container.addView(overlayView, layoutParams);
		
		setContentView(viewContainer);
		
		
//		Intent intent = new Intent(this, MultiTouchActivity.class);
//		this.startActivity(intent);
	}

	
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) 
	{
        super.onCreateOptionsMenu(menu);
        
        menu.add(0, MENU_COLOR, 0, "Color");
        menu.add(0, MENU_CLEAR, 0, "Clear");
        menu.add(0, MENU_ERASE, 0, "Erase");
        menu.add(0, MENU_STROKE_WIDTH_10, 0, "StrokeWidth 10");
        menu.add(0, MENU_STROKE_WIDTH_20, 0, "StrokeWidth 20");
        menu.add(0, MENU_ADD_STICKER, 0, "Add Sticker");
        
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
    {
        super.onPrepareOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
    	switch (item.getItemId())
		{
			case MENU_COLOR:
				new ColorPickerDialog(this, this, 0xFFFF0000).show();
	            return true;

			case MENU_CLEAR:
				drawingView.clear();
	            return true;
	            
			case MENU_ERASE:
				drawingView.setErase();
	            return true;
	            
			case MENU_STROKE_WIDTH_10:
				drawingView.setStroke(10.0f);
	            return true;
	            
			case MENU_STROKE_WIDTH_20:
				drawingView.setStroke(20.0f);
	            return true;
	            
			case MENU_ADD_STICKER:
				addSticker();
	            return true;
	            
			default:
				return super.onOptionsItemSelected(item);
		}
    }

    @Override
    public void colorChanged(int newColor) 
    {
        drawingView.setColor(newColor);
    }

    
    int count = 1;
    int resourceID;
    private void addSticker()
	{
		if (count == 1)
			resourceID = R.drawable.cat;
		else if (count == 2)
			resourceID = R.drawable.diamond;
		else if (count == 3)
		{
			resourceID = R.drawable.pen;
			count = 0;
		}

		count++;
		
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceID);
		bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
        RZStickerView stickerView = new RZStickerView(this, bitmap);
		
		layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
		viewContainer.addView(stickerView, layoutParams);
	}
}
